package leetcode_1_200;

public class LeeCode_122 {
    public static void main(String[] args) {
        System.out.println(maxProfit(new int[]{7, 1, 5, 3, 6, 4}));
        System.out.println(maxProfit(new int[]{1,2,3,4,5}));
        System.out.println(maxProfit(new int[]{2,1,2,0,1}));
        System.out.println(maxProfit(new int[]{2,1,2,1,0,1,2}));
    }
    private static int maxProfit(int[] prices) {
        int len = prices.length;
        int have = 0;
        boolean hav = false;
        int res = 0;
        for (int i = 0; i < len; i++) {
            if (i == len - 1){
                if (i - 1 >= 0 && prices[i] > prices[i - 1]){
                    res += prices[i] - have;
                }
                break;
            }
            if (hav && prices[i] > have){
                res += prices[i] - have;
                have = 0;
                hav = false;
            }
            if (!hav && prices[i] < prices[i + 1]){
                have = prices[i];
                hav = true;
            }
        }
        return res;
    }
}
